<template>
  <div class="app-container">
    <div class="main">
      <div class="filter-container g-bgc-fill-el-input">
        <div class="filter-left">
          <!-- <el-select v-model="listQuery.brand" placeholder="所属品牌" clearable @change="comHandleFilterSelectChange"
            style="width: 90px" class="filter-item">
            <el-option v-for="(item, index) in brandOptions" :key="'br' + index" :label="item.menufacturer"
              :value="item.id" />
          </el-select> -->
          <el-date-picker v-model="listQuery.date" type="daterange" @change="comHandleFilterDateChange"
            format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" style="width: 208px;" range-separator=""
            start-placeholder="开始时间" end-placeholder="结束时间">
          </el-date-picker>
          <np-input-search v-model="listQuery.keyword" placeholder="搜索用户昵称、手机尾号" style="width: 360px;"
            @all-search="comHandleFilterAllSearch" />
        </div>
      </div>

      <el-table class="g-el-table" :data="list" v-loading="listLoading">
        <el-table-column label="头像" prop="deviceCode" width="80PX">
          <div slot-scope="{row}" class="profile_picture_warp">
            <img :src="profilePictureSrc(row)" v-if="profilePictureSrc(row)">
            <div v-else></div>
          </div>
        </el-table-column>
        <el-table-column label="家庭管理员" width="120px">
          <template slot-scope="{row}">
            <span>{{ row.nickName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号" min-width="150px">
          <template slot-scope="{row}">
            <span>{{ row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属品牌" width="110px">
          <template slot-scope="{row}">
            <span>{{ row.nickName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所在地区" width="80px">
          <template slot-scope="{row}">
            <span>{{ row.address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="95">
          <template slot-scope="{row}">
            <span>{{ row.createdDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" width="95">
          <template slot-scope="{row}">
            <span>{{ row.updatedDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="230" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button type="text" @click="handleViewUser(row)">查看用户</el-button>
          </template>
        </el-table-column>
      </el-table>

      <np-pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
        @pagination="getList" />

    </div>
  </div>
</template>

<script>
import { getMenufacturers } from "@/api/family-and-users-management/index"
import { getAllUsers } from '@/api/family-and-users-management/user'

export default {
  name: 'ComplexTable',
  // directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,

        brand: undefined,
        keyword: undefined,

        date: undefined,
        startDate: undefined,
        endDate: undefined,
      },
      brandOptions: [],
      p2pAddressOptions: [],
    }
  },
  computed: {
  },
  created() {
    this.getList()
    getMenufacturers({ pageNum: 1, pageSize: 1000 }).then(res => {
      if (res.code === 200) {
        this.brandOptions = res.data.list || []
      }
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      let pans = { ...this.listQuery }
      delete pans.date
      getAllUsers(pans).then(res => {
        if (res.code === 200) {
          this.list = res.data.list
          this.total = res.data.total
        }

        // Just to simulate the time of the request
        this.listLoading = false
      })
    },
    comHandleFilterAllSearch(e, type) {
      this.listQuery.pageNum = 1
      this.getList()
    },
    comHandleFilterSelectChange(e) {
      this.listQuery.pageNum = 1
      this.getList()
    },
    comHandleFilterDateChange(e) {
      this.listQuery.pageNum = 1
      this.listQuery.startDate = e ? e[0] : ''
      this.listQuery.endDate = e ? e[1] : ''
      this.getList()
    },
    profilePictureSrc(row) {
      try {
        let base_url = process.env.VUE_APP_IMG_BASE_URL,
          url = row.headUrl;
        if (url) {
          return `${base_url}/${url}`
        }
      } catch (error) { }
      return ""
    },
    handleViewUser(row) {
      // id 家庭圈成员用户id
      // userId 用户id
      this.$router.push({
        path: `/family-and-users/user/member`,
        query: {
          id: row.id || '',
          deviceCode: row.deviceCode || '',
        }
      })
    },
  }
}
</script>
<style scoped lang="scss">
.app-container {
  height: 100%;
  overflow: auto;
}

.profile_picture_warp {
  display: flex;
  justify-content: center;
  align-items: center;

  @mixin com_img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    flex-shrink: 0;
  }

  img {
    @include com_img;
  }

  div {
    @include com_img;
    background: #CCCCCC;
  }
}

.main {
  background-color: #fff;
  border-radius: 16px;
  padding: 20px 20px 28px;
}
</style>